package com.whq.thrift.api.web.admin.db.baseconf;

import java.util.Map;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class ModuleInstanceSqlProvider {
	public String getSelectRecordsSql(Map<String, Object> parameter) {
        SqlBuilder.BEGIN();
        SqlBuilder.SELECT("*");
        SqlBuilder.FROM("t_module_instance");
        
        if ( (parameter.get("moduleId") != null) 
        		&& (StringUtil.isNotBlank(parameter.get("moduleId").toString()))
        		) {
        	SqlBuilder.WHERE("module_id like #{moduleId}");
        }
        
        SqlBuilder.ORDER_BY("module_id asc");
        return SqlBuilder.SQL() + " limit #{beginIndex}, #{maxCount} ";
    }
	
	public String getSelectCountSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
        SqlBuilder.SELECT("count(*)");
        SqlBuilder.FROM("t_module_instance");
        
        if ( (parameter.get("moduleId") != null) 
        		&& (StringUtil.isNotBlank(parameter.get("moduleId").toString()))
        		) {
        	SqlBuilder.WHERE("module_id like #{moduleId}");
        }
        
        return SqlBuilder.SQL();
    }
	
	public String getUpdateSql(ModuleInstance moduleInstance) {
        SqlBuilder.BEGIN();
        SqlBuilder.UPDATE("t_module_instance");
        
        /*SqlBuilder.SET("instance_id = #{instanceId}");*/
        
        if (moduleInstance.getIp() != null) {
        	SqlBuilder.SET("ip = #{ip}");
        }
        
        if (moduleInstance.getPort() >= 0) {
        	SqlBuilder.SET("port = #{port}");
        }
        
        if (moduleInstance.getStatus() >= 0) {
        	SqlBuilder.SET("status = #{status}");
        }
        
        if (moduleInstance.getUpdateTime() != null) {
        	SqlBuilder.SET("update_time = #{updateTime}");
        }
        
        SqlBuilder.WHERE("instance_id = #{instanceId}");
        return SqlBuilder.SQL();
    }

}
